package com.aptech.util;



import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Paging {
    private int rowPerPage;
    private int index;
    private List list;

    public Paging(List list, int index, int rpp) {
        this.rowPerPage = rpp;
        this.index = index;
        this.list = list;
    }

    //create a list from index to index + rowPerpage
    public List getListByIndex() {
        List newList = new ArrayList();
        int to = index + rowPerPage;
        
        //adjust data if exceed
        if (index > list.size()) {
            index = list.size();
        }

        if (to > list.size()) {
            to = list.size();
        }

        for (int i = index; i < to;i++) {
            newList.add(list.get(i));
        }
        return newList;
    }

    //get collection of integer value to display pages
    public Collection getPages() {
        Collection pList = new ArrayList();
        int pages = list.size()/rowPerPage;
        if (list.size() % rowPerPage != 0) {
            pages++;
        }
        for (int i = 0; i < pages; i++) {
            pList.add(i+1);
        }
        return pList;
    }
    /**getter setter area*/
    public int getRowPerPage() {
        return rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
    /**end getter setter area*/
}
